package com.br.negocio.web.backbean;

import java.io.Serializable;

import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.br.negocio.controller.ControllerServico;
import com.br.negocio.vo.ServicoVO;
import com.br.negocio.web.formbean.ServicoFormBean;

@Controller("servicoBackBean")
@Scope("session")
public class ServicoBackBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Resource
	private ControllerServico controllerServico;
	
	private ServicoFormBean servicoFormBean;

	public ServicoBackBean() {
		super();
		this.servicoFormBean = new ServicoFormBean();
	}

	public ServicoFormBean getServicoFormBean() {
		return this.servicoFormBean;
	}

	public void setServicoFormBean(ServicoFormBean servicoFormBean) {
		this.servicoFormBean = servicoFormBean;
	}
	
	// METODOS

	public void obterListaServico() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.servicoFormBean.setListaServico(this.controllerServico.obterListaServico());
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao obter a lista de servico: " + e.getMessage()));
		}
	}
	
	public void manterServico() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			this.controllerServico.atualizarServico(this.servicoFormBean.getServicoManter());
			this.servicoFormBean.setIsDisabled(true);
			context.addMessage(null, new FacesMessage("Sucesso: Inserido realizado com sucesso!"));
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao manter o servico: " + e.getMessage()));
		}

	}
	
	public void excluirServico() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		try {
			if (this.getServicoFormBean().getServicoManter().getCodigo() != 0) {
				this.controllerServico.removerServico(this.servicoFormBean.getServicoManter());
				if (!this.servicoFormBean.getListaServico().isEmpty()) {
					this.servicoFormBean.setListaServico(this.controllerServico.obterListaServico());
				}
				this.servicoFormBean.setServicoManter(new ServicoVO());
				context.addMessage(null, new FacesMessage("Sucesso: Servico excluido com sucesso!"));
			}
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage("Erro ao excluir o servico: " + e.getMessage()));
		}
	}
	
	// LISTENERS
	
	public void listenerIrInserirServico(ActionEvent evento) {
		this.servicoFormBean.setServicoManter(new ServicoVO());
		this.servicoFormBean.getServicoManter().setTipoServico(1);
		this.servicoFormBean.setIsDisabled(false);
		this.servicoFormBean.setHead("Cadastrar Servico");
	}
	
	public void listenerIrExcluirServico(ActionEvent evento) {
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoServicoExcluir");
		int codigo = Integer.valueOf((Integer)comp.getValue());
		
		if (codigo != 0) {
			ServicoVO servicoVO = new ServicoVO();
			servicoVO.setCodigo(codigo);
			this.servicoFormBean.setServicoManter(this.controllerServico.obterServico(servicoVO));
		}
		this.servicoFormBean.setHead("Excluir Servico");
		this.servicoFormBean.setIsDisabled(true);
	}
	
	public void listenerIrEditarServico(ActionEvent evento) {
		UIParameter comp = (UIParameter) evento.getComponent().findComponent("codigoServicoEditar");
		int codigo = Integer.valueOf((Integer)comp.getValue());
		
		if (codigo != 0) {
			ServicoVO servicoVO = new ServicoVO();
			servicoVO.setCodigo(codigo);
			this.servicoFormBean.setServicoManter(this.controllerServico.obterServico(servicoVO));
		}
		this.servicoFormBean.setHead("Editar Servico");
		this.servicoFormBean.setIsDisabled(false);
	}

}